#include    "VSounds.h"
#include    <QApplication>

VSounds::VSounds()
{
    this->setFixedSize(260, 100);

    CSounds::play_pause();

    this->window = new QLabel(this);
    this->window->move(10,10);
    this->window->setFixedSize(240,90);

    this->label_slider_sound_less = new QLabel("-", this);
    this->label_slider_sound_less->move(20,12);
    this->label_slider_sound_less->setStyleSheet("font-size : 18px");

    this->label_slider_sound_more = new QLabel("+", this);
    this->label_slider_sound_more->move(231,12);
    this->label_slider_sound_more->setStyleSheet("font-size : 17px");

    this->volume_slider = new Phonon::VolumeSlider(this);
    this->volume_slider->setAudioOutput(CSounds::background_playing->audio_output);
    CSounds::background_playing->audio_output->setVolume(CSounds::background_volume);
    this->volume_slider->setGeometry(30,10,200,30);
    this->volume_slider->setMuteVisible(false);

    this->seek_slider = new Phonon::SeekSlider(this);
    this->seek_slider->setMediaObject(CSounds::background_playing->media_object);
    this->seek_slider->setGeometry(20,72,220,30);

    this->play_button = new QPushButton(this);
    this->play_button->move(161, 43);
    this->play_button->resize(25,25);
    this->play_button->setIcon(QIcon(MPictures::get_instance()->m_pictures.value("pause")));

    this->stop_button = new QPushButton(this);
    this->stop_button->move(214,43);
    this->stop_button->resize(25,25);
    this->stop_button->setIcon(QIcon(MPictures::get_instance()->m_pictures.value("stop")));

    this->mute_button = new QPushButton(this);
    this->mute_button->move(107,43);
    this->mute_button->resize(25,25);
    this->mute_button->setIcon(QIcon(MPictures::get_instance()->m_pictures.value("mute")));

    this->browse_button = new QPushButton("Parcourir", this);
    this->browse_button->move(20, 43);
    this->browse_button->resize(60,25);

    this->clicked_play = 0;
    this->clicked_mute = 0;
    QObject::connect(this->play_button, SIGNAL(clicked()), this, SLOT(play_pause()));
    QObject::connect(this->stop_button, SIGNAL(clicked()), this, SLOT(stop()));
    QObject::connect(this->mute_button, SIGNAL(clicked()), this, SLOT(mute()));
    QObject::connect(this->browse_button, SIGNAL(clicked()), this, SLOT(open_file()));

    QObject::connect(CSounds::background_playing->audio_output, SIGNAL(volumeChanged(qreal)), this, SLOT(update_background_volume(qreal)));

    CDesign::get_instance()->change_color_widget(this);
    CDesign::get_instance()->change_color_label_border(this->window);
    CDesign::get_instance()->change_color_button(this->play_button);
    CDesign::get_instance()->change_color_button(this->stop_button);
    CDesign::get_instance()->change_color_button(this->mute_button);
    CDesign::get_instance()->change_color_button(this->browse_button);
}

VSounds::~VSounds()
{
    CSounds::clear_background_playing();
    delete this->volume_slider;
    delete this->seek_slider;
    delete this->play_button;
    delete this->stop_button;
    delete this->mute_button;
    delete this->browse_button;
}

void    VSounds::play_pause()
{
    if(CSounds::play_pause() == 1)
    {
        this->seek_slider->setMediaObject(CSounds::background_playing->media_object);
        this->volume_slider->setAudioOutput(CSounds::background_playing->audio_output);
        CSounds::background_playing->audio_output->setVolume(CSounds::background_volume);
        QObject::connect(CSounds::background_playing->audio_output, SIGNAL(volumeChanged(qreal)), this, SLOT(update_background_volume(qreal)));
    }

    if(this->clicked_play == 0)
    {
        this->play_button->setIcon(QIcon(MPictures::get_instance()->m_pictures.value("play")));
        this->clicked_play = 1;
    }

    else
    {
        this->play_button->setIcon(QIcon(MPictures::get_instance()->m_pictures.value("pause")));
        this->clicked_play = 0;
    }
}

void    VSounds::stop()
{
    CSounds::stop();
    this->play_button->setIcon(QIcon(MPictures::get_instance()->m_pictures.value("play")));
    this->mute_button->setIcon(QIcon(MPictures::get_instance()->m_pictures.value("mute")));
    this->clicked_play = 1;
    this->clicked_mute = 0;
}

void    VSounds::mute()
{
    CSounds::mute();

    if(this->clicked_mute == 0)
    {
        this->mute_button->setIcon(QIcon(MPictures::get_instance()->m_pictures.value("unmute")));
        this->clicked_mute = 1;
    }

    else
    {
        this->mute_button->setIcon(QIcon(MPictures::get_instance()->m_pictures.value("mute")));
        this->clicked_mute = 0;
    }
}

void    VSounds::update_background_volume(qreal volume)
{
    CSounds::update_background_volume(volume);
}

void    VSounds::open_file()
{
    this->file = new QFileDialog(this);
    CSounds::song_name = this->file->getOpenFileName(this, tr("Choisissez votre musique"), "C:/", tr("Audio Files (*.mp3 *.ogg *.flac"));
    CSounds::stop();
}
